let gp

let router = {
	to (page, {params = {}, events = {}, subpack = ""} = {}) {
		let url = concatUrlPamras(page, params, subpack)
		
		uni.navigateTo({ url, events })
	},
	replace(page, params = {}) {
		let url = concatUrlPamras(page, params)
		uni.redirectTo({url})
	},
	back () {
		uni.navigateBack()
	},
	toPageCousult (id) {
		this.to('consult', { params: { id } })
	},
	toPageFileRecord (type, petId) {
		this.to('file_record', { params: { type, petId } })
	}
}

// 拼接路由和参数
function concatUrlPamras (page, params, subpack) {
	
	let paramsStr = ""
	for(let p in params) {
		paramsStr += `${p}=${params[p]}&`
	}
	
	let url = `/pages/${page}/index?${paramsStr}`
	
	if (subpack) {
		url = `/subpackages/${subpack}` + url;
	}
	
	return url
}

function initRouter (app) {
	gp = app.config.globalProperties
	gp.$router = router
}

export { initRouter }